//
//  ForgetViewController.m
//  随便点
//
//  Created by MWJ on 15/3/11.
//  Copyright (c) 2015年 qfsoft. All rights reserved.
//

#import "ForgetViewController.h"
#import "LoginViewController.h"
#define Kyanzhengma @"http://www.51anys.com:8080/waimaiwang/user/validate/forget"
#define Kxiugai @"http://www.51anys.com:8080/waimaiwang/user/forgetPwd"
@interface ForgetViewController (){
    NSString *str;
    NSString *str1;
    NSString *sentstring;
    NSString *registerstring;
    UIAlertView *_alert;
}

@end

@implementation ForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed: @"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = item;
    // Do any additional setup after loading the view.
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)yanzheng{
    NSURL *url = [NSURL URLWithString:Kyanzhengma];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    str=[NSString stringWithFormat:@"phone=%@",self.phone.text];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        sentstring = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:sentstring waitUntilDone:YES];
        int intString = [sentstring intValue];
        if (intString==0) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"验证码发送失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
        }else{
            UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"验证码已发送" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert1 show];
        }
    }];
}
-(void)xg{
    NSURL *url = [NSURL URLWithString:Kxiugai];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    str1=[NSString stringWithFormat:@"phone=%@&number=%@&password=%@",self.phone.text,self.yanzhengma.text,self.password1.text];
    NSData *data = [str1 dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        registerstring = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:registerstring waitUntilDone:YES];
        int intStr = [registerstring intValue];
        if (intStr == 1) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            _alert = alert;
            [alert show];
        }else{
            if (intStr == 0) {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alert show];
            }else{
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该手机号不存在" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alert show];
            }
        }
    }];
}
-(void)refreshData:(NSArray *)sender{
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0&&alertView==_alert) {
        LoginViewController *lgvc = [self.storyboard instantiateViewControllerWithIdentifier:@"loginVC"];
        [self.navigationController pushViewController:lgvc animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)fasong:(id)sender {
    if ([self.phone.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入手机号码" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }else{
        [self yanzheng];
    }
}

- (IBAction)xiugai:(id)sender {
    if ([self.yanzhengma.text isEqualToString:@""]||[self.password1.text isEqualToString:@""]||[self.password2.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入完整信息" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }else{
        [self xg];
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.phone resignFirstResponder];
    [self.yanzhengma resignFirstResponder];
    [self.password1 resignFirstResponder];
    [self.password2 resignFirstResponder];
}
@end
